package com.mdinfo.gw.door.service.impl

import com.mdinfo.base.common.bean.CommonResult
import com.mdinfo.base.common.bean.DataGrid
import com.mdinfo.base.common.bean.Ref
import com.mdinfo.base.utils.SystemUtil
import com.mdinfo.base.utils.TimeUtil
import com.mdinfo.gw.door.bean.DoorAccess
import com.mdinfo.gw.door.bean.DoorEvent
import com.mdinfo.gw.door.mapper.DoorEventMapper
import com.mdinfo.gw.door.mapper.DoorAccessMapper
import com.mdinfo.gw.door.param.DoorEventListQueryParam
import com.mdinfo.gw.door.service.IDoorEventService
import com.mdinfo.gw.door.vo.DoorEventListQueryVo
import org.apache.commons.lang3.time.DateUtils
import org.springframework.stereotype.Service
import java.util.*

/**
 * Created by Administrator on 2018/1/8.
 */
@Service
class DoorEventServiceImpl(
        val eventMapper: DoorEventMapper,
        val doorMapper: DoorAccessMapper
) :IDoorEventService {
    override fun report(doorId: Int, type: Int): CommonResult<Void> {

        return Ref.ok()
    }

    /**表格条件查询*/
    override fun listQuery(param: DoorEventListQueryParam)
            : CommonResult<DataGrid<DoorEventListQueryVo>>{


        var startTime:Date? = null
        var endTime:Date? = null

        if(param.startTime!=null){
            startTime = TimeUtil.parse1(param.startTime)
            endTime = TimeUtil.parse1(param.endTime)
            if(endTime != null){
                endTime = DateUtils.addDays(endTime,1)
            }
        }

        var info = SystemUtil.getInfo()
        var list = eventMapper.listQuery(param.start!!,
                param.num!!,
                param.doorId,
                param.doorAccessId,
                param.cardId,
                param.deptId,
                param.key,
                param.doorEvent,
                startTime,
                endTime,
                info.deptIdList)
        var count = eventMapper.countListQuery(param.doorId,
                param.doorAccessId,
                param.cardId,
                param.deptId,
                param.key,
                param.doorEvent,
                startTime,
                endTime,
                info.deptIdList)
        var dataGrid = DataGrid<DoorEventListQueryVo>()
        dataGrid.rows = list
        dataGrid.total = count
        return Ref.ok(dataGrid)
    }
}